package com.xmfh.wemedia.controller;


import com.xmfh.wemedia.mapper.DdMicroMapper;
import com.xmfh.wemedia.model.DdMicro;
import com.xmfh.wemedia.model.DdSubMicro;
import com.xmfh.wemedia.model.DdUserBaseMicro;
import com.xmfh.wemedia.service.IDdMicroService;
import com.xmfh.wemedia.service.IDdSubMicroService;
import com.xmfh.wemedia.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 微头条表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-04-03
 */
@RestController
@RequestMapping("/wemedia/ddMicro")
public class DdMicroController {
    @Autowired
    private IDdMicroService service;

    @Autowired
    private IDdSubMicroService subMicroService;

    @Autowired
    DdMicroMapper mapper;

    /**
     * 新增一条微头条
     *
     * @param micro
     * @return
     */
    @RequestMapping("releaseNewDynamic")
    public JsonResult<Void> releaseNewDynamic(@RequestBody DdMicro micro) {
        // 调用micro业务层的addDynamic方法来进行处理
        int result = service.addDynamic(micro);
        System.out.println(result);
        // 看看是否有图片，有的话就调用subMicro业务层的方法来处理，没有就不管
        if (micro.getHasImage() == 1) {
            DdSubMicro ddSubMicro = new DdSubMicro();
            ddSubMicro.setMicroId(micro.getId());
            ddSubMicro.setImgUrl(micro.getImageArrStr());
            subMicroService.save(ddSubMicro);
        }
        if (result > 0) {
            return JsonResult.ok();
        }
        return JsonResult.fail(JsonResult.State.ERROR_UNKNOWN, "服务器正忙....");
    }

    /**
     * 删除微头条
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    public JsonResult<Void> deleteMicro(@PathVariable int id) {
        boolean b = service.removeById(id);
        if (b) {
            return JsonResult.ok();
        } else {
            return JsonResult.fail(JsonResult.State.ERROR_UNKNOWN,"删除失败");
        }
    }

    /**
     * 获取微头条列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("{userId}/list/{pageNum}/{pageSize}")
    public JsonResult<List<DdMicro>> microList(@PathVariable Integer pageNum,@PathVariable Integer pageSize,@PathVariable Integer userId){
        List<DdMicro> microList = service.getByUserID(pageNum, pageSize, userId);
        return JsonResult.ok(microList);
    }

    /**
     * 获取微头条列表页数
     *
     * @return
     */
    @RequestMapping("/loadPage/{userId}")
    public JsonResult<Integer> loadPage(@PathVariable Integer userId) {
        int count = service.selectCount(userId);
        return JsonResult.ok(count);
    }

    /**
     * 获取个人主页中微头条分页信息
     *
     * @param session 网页缓存
     * @return 个人微头条集合
     */
    @GetMapping("/home_page/getPersonalMicro")
    public JsonResult<List<DdUserBaseMicro>> getPersonalMicro(HttpSession session) {
        return JsonResult.ok(
                mapper.getPersonalMicroById(
                        DdUserController.getCurrentUser(session).getId())
        );
    }
}
